package com.qf.factory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 自定义工厂
 * MyFactory.getBean()
 * @author lixu
 */
public class MyFactory {

    public MyFactory() {
    }

    //加载原材料的
    private static Properties properties = new Properties();
    //负责加载properties文件中的内容
    static {
        //输入流：将磁盘上的内容读取到内存中 叫输入
        //D:\IdeaProjects\gp14\01springDemo\src\main\resources\bean.properties
        //InputStream inStream = new FileInputStream("D:\\IdeaProjects\\gp14\\01springDemo\\src\\main\\resources\\bean.properties");
        InputStream rsa = MyFactory.class.getResourceAsStream("/bean.properties");
        try {
            properties.load(rsa);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //生产  实例化：任何一个实现类对象  名称
    public static Object getBean(String name){
        //全限名
        String value = properties.getProperty(name);
        try {
            Class<?> aClass = Class.forName(value);
            return aClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //重载方法  参数传入的类型
    public static Object getBean(Class<?> aClass){
        try {
            return aClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
